package com.scalar.chiptrack.operations.dataentry.lotTransfer.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.lotTransfer.forms.TransferMaterialForm;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 10 Feb, 2010
 * Time: 6:43:40 PM
 */
public class TransferMaterialSearchDetailsCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/transferMaterial.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {
       TransferMaterialForm transferMaterialForm = (TransferMaterialForm) form;
        String action                       = request.getParameter ( "action" );

        HashMap<String,String> errors       = new HashMap<String,String>();
        HashMap<String,String> info         = new HashMap<String,String>();

        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );
        String locationType = null;
        HttpSession mSession = request.getSession();
        User user            = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.TRANSFER_MATERIAL , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }

        locationType = NewProductDAO.getBaseProcessName( null, transferMaterialForm.getProcessType() );

        try
        {
            transferMaterialForm.setWipLotsList( null );

            if ( user.getVendorId() > 0 ) // for VENDOR INTERFACE
            {
                transferMaterialForm.setLocationId(0);
                if (transferMaterialForm.getLocationId() == 0 )
                {
                   ArrayList<LabelValueBean> vendorList =  VendorDAO.getInventoryLocationsByProcessByUserType( null, locationType , user.getVendorId() );
                    if ( vendorList != null && vendorList.size() > 0  )
                    {
                        transferMaterialForm.setLocationId( Integer.parseInt ( ( (LabelValueBean)vendorList.get(0) ).getValue() ) );
                    }
                    else
                    {
                          transferMaterialForm.setLocationId(0);
                    }
                }
            }
            if (LOCATION_TYPE_DIE_BANK.equalsIgnoreCase(transferMaterialForm.getProcessType()) )
            {
                 request.setAttribute("lotsList", DataEntryDAO.loadAllDieBankWipLots( 0, transferMaterialForm.getLocationId(), 0, false, false ) );
            }
            else
            {
                request.setAttribute("lotsList", DataEntryDAO.getAllRunningLots( transferMaterialForm.getProcessType(), 0, transferMaterialForm.getLocationId(), 0 ,false) );
            }

            request.setAttribute("processTypes", NewProductDAO.loadAllProcessesFromDB(null, false));
            request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( transferMaterialForm.getProcessType() ) );
            request.setAttribute("locationList", VendorDAO.getInventoryLocationsByProcessByUserType( null, locationType, user.getVendorId() ) );

            request.setAttribute( "action", action );
        }
        catch( Exception e )
        {
            request.setAttribute("processTypes", NewProductDAO.loadAllProcessesFromDB(null, false));
            errors.put( "Unable to get Inventory Lots For Scrap", null);
            e.printStackTrace();
        }
        return mapping.findForward( "transferMaterial" );
     }
}
